Skip to content

fix(studio): recover a draft only for the project and file it was saved for - #7891

Closed
michaelwang11394 wants to merge 6 commits into
mainfrom
sec-studio-recovery-drafts-1116
Closed

michaelwang11394 wants to merge 6 commits into
mainfrom
sec-studio-recovery-drafts-1116

Conversation

@michaelwang11394

@michaelwang11394 michaelwang11394 commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Studio keeps unsaved recovery drafts (a save that conflicted with an outside edit, or failed) in an IndexedDB store. That store is shared by every project served from the same origin. The loader returned whatever record sat under a key, and the caller then used the record's own filePath. So a record written under one project's key could name a different project or file, and Studio would offer to restore its content over that file.

loadExternalConflictSnapshot now returns a record only when all of these hold:

  • its projectId and filePath equal the key it was read under,
  • its createdAt is a number less than 30 days old.

A record that fails these checks is treated as absent. It is deleted in the same IndexedDB transaction that read it, so a fresh draft another tab saves at the same moment is never removed. A draft Studio saved for this project and file is recovered exactly as before.

Testing

fails without the fix: packages/studio/src/utils/externalConflictStorage.test.ts "keeps a fresh draft another tab saves while an expired one is being cleared".
fails without the fix: packages/studio/src/utils/externalConflictStorage.test.ts "refuses and deletes a stored record with another project's id".

  • packages/studio: env -u AWS_PROFILE npx vitest run src/utils/externalConflictStorage.test.ts src/hooks/useExternalFileChangeCoordinator.test.tsx (63 passed).
  • New tests write records straight into the store under one project's key that name another project or another file. Studio shows no banner, does not pause the preview, offers no overwrite, and deletes the record. Studio's own saved draft is still recovered.
  • Pre-commit hooks pass: lint, format, typecheck and the unused-export audit.

No visible change

No screen changes. A genuine draft is recovered exactly as before; only records that name another project or file, or are over 30 days old, are no longer offered.

@github-actions

github-actions Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Edit accuracy: accurate 267 (base branch 267), smooth 97 of those

The gate passes.
Smoothness is reported in the artifact, not gated. A case fails only if it fails 2 of 3 runs.

Quarantined, measured but not gated (8)

Unstable (1)

  • seqab-none-center-r0-root-z100: tracking 0.03, pressJump 0, drop 0, reload 0, render 0.02, renderKey -, undo true, teleport true / tracking 0.03, pressJump 0, drop 0, reload 0, render 0.02, renderKey -, undo true, teleport true / tracking 0.03, pressJump 0, drop 0, reload 0, render 0.02, renderKey -, undo true, teleport true

@jrusso1020 jrusso1020 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed at 3ae73e59dd040f65ede5c52ca59eaf10e7fead52.

The exact project/file comparison closes the cross-key restoration case, but the new cleanup can erase a legitimate replacement draft.

Important: packages/studio/src/utils/externalConflictStorage.ts:165–171 reads in one transaction and deletes in another. Tab A reads an expired record; tab B saves a fresh draft under that key; A then unconditionally deletes B's draft. A fake-indexeddb regression that queues B's write during A's read reproduced the loss: expected the fresh draft, received undefined. Read, validate and conditionally delete within one readwrite transaction. A local diagnostic implementation of that change passes the same regression; source was restored after testing.

Validation: focused storage run using a minimal Vitest config: 20 passed and the new race regression failed; diagnostic fix: 1 passed. The normal Studio config could not load an unbuilt studio-server package, so this is not a full Studio suite result.

Pre-existing follow-up: there is still no record-shape validator, and timestamp arithmetic accepts numeric strings. The identity fix is real, but the Slack description's shape-validation claim is not implemented. Validate kind, content/version fields and a finite numeric timestamp separately.

Need: yes · Size: ok · Reuse: existing IndexedDB transaction helper · Simpler: keep validation and deletion in one transaction · Structure: fine (checked storage writer and recovery coordinator; no split)
Risk: medium — the new two-transaction cleanup can delete unsaved user work.

Verdict: REQUEST CHANGES
Reasoning: Make expired/mismatched-record cleanup atomic with its read so another tab's valid draft survives.

— Review by Rames

@michaelwang11394

michaelwang11394 commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks, Rames, good catch. In 559a654 the loader reads, checks and deletes inside one readwrite transaction, so another tab's fresh draft can't be removed between the read and the delete. Your race is now a regression test: "keeps a fresh draft another tab saves while an expired one is being cleared". It fails on the previous commit. The age check also requires a numeric createdAt now, so a numeric string is refused.

On shape validation: you're right that my Slack note overstated it. I dropped the shape checks to keep this change small. This PR closes the cross-key case only. A record written under the right key by other code on the same origin is closed by running the preview sealed (#7846). I'll do kind and field validation as a separate follow-up.

@michaelwang11394

Copy link
Copy Markdown
Contributor Author

Closing this as superseded. #7904 (merged) fixed the same problem on main a different way: recovery drafts now live on each project's own Studio server, in the project's .hyperframes/recovery folder, not in the browser's shared IndexedDB. The loader also refuses a draft whose projectId or filePath differs from the key. That removes the IndexedDB store this PR hardened, including the read-then-delete race Rames found. SECOPS-1427 is Done and finding find_01Y7Ws9dPSSpHXkqUvsjfAxG is resolved as fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants